﻿
Imports System.IO
Imports std
Imports xmlctrl
Imports xhtmlctrl
Imports webctrl
Imports storoomc

Public Class searchpage
    Inherits cacheControlProcessor
    'handle /search, render tag list or other terms for user to select

    Public Const _pathidentity As String = "/search"

    Protected Overrides Function retiredticks() As Long
        Return 10 * 60 * 1000 * 1000
    End Function

    Public Overrides Function pathidentity() As String
        Return _pathidentity
    End Function

    Protected Overrides Function processImpl(ByVal context As httpContext, ByRef control As xmlcontrol) As Boolean
        Dim buff As bytes = Nothing
        If storoom.read(rashare.tagsname, buff) Then
            Dim page As xhtmlpage = Nothing
            page = context.createControl(New xhtmlpage(Nothing))
            Dim tag As String = Nothing
            tag = encoding.utf8.GetString(buff.data)
            Dim temp As vector(Of String) = Nothing
            strsepar(tag, character.newline, temp, True)
            'TODO, also not a good coding here, but currently, some tags are seperated by \r\n, some are \n only
            Dim tags As [set](Of String) = Nothing
            tags = New [set](Of String)
            Dim i As Int64
            For i = 0 To temp.size() - 1
                kickNewlineChar(temp(i))
                tags.insert(temp(i))
            Next

            page.title().text = applicationSign() + " - search"
            page.addcsslink(staticrequest.generateStaticfileLinkUrl("searchpage.css"))

            Dim label As label = Nothing
            label = context.createControl(New label(Nothing))
            label.text = "please select prefered categories."
            page.body().addchild(label)

            Dim form As form = Nothing
            form = context.createControl(New form(Nothing))
            form.action = strmid(rssrequest._pathidentity, strlen(network.pathSeperator))
            page.body().addchild(form)

            Dim [select] As [select] = Nothing
            [select] = context.createControl(New [select](Nothing))
            [select].name = rssrequest.query.tag
            [select].multiple = True
            form.addchild([select])

            Dim it As [set](Of String).iterator = Nothing
            it = tags.begin()
            While it <> tags.end()
                Dim [option] As [option] = Nothing
                [option] = context.createControl(New [option](Nothing))
                [option].value = (+it)
                [option].text = (+it)
                If rnd(0, 2, True) Then
                    [option].selected = True
                End If
                [select].addchild([option])

                it += 1
            End While

            Dim input As input = Nothing
            input = context.createControl(New input(Nothing))
            input.type = xhtmlctrl.input.type_enum.submit
            input.value = "submit"
            form.addchild(input)

            control = page
            Return True
        Else
            Return False
        End If
    End Function
End Class
